package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/contract")
public class ConteractController extends BaseController {

    @Reference
    private ContractService contractService;

    /*
        分页查询所有数据  连接地址 ：/cargo/contract/list.do
     */
    @RequestMapping("/list")
    public String findAll(
            @RequestParam(defaultValue = "0")int page,
            @RequestParam(defaultValue = "5")int size){
        //创建example
        ContractExample example = new ContractExample();
        //通过example创建criteria对象
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //获取登录用户
        User user = getLoginUser();
        //获取人员部门信息
        Integer degree = user.getDegree();
        //判断人员级别信息拼接查询sql
        if (degree==4){
            //向criteria中设置查询条件
            criteria.andCreateByEqualTo(user.getId());
        }else if (degree==3){
            criteria.andCreateDeptEqualTo(user.getDeptId());
        }else if (degree==2){
            criteria.andCreateDeptLike(user.getDeptId()+"%");
        }
        //发起调用
        //设置排序
        example.setOrderByClause("create_time DESC");
        PageInfo info = contractService.findAll(page, size, example);
        //将查询的数据保存到Requestrian域中
        request.setAttribute("page",info);
        //跳转页面
        return "cargo/contract/contract-list";
    }

    /*
        新建购销合同(跳转到新增页面）
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "cargo/contract/contract-add";
    }

    /*
        新增或修改
        参数：contract
        返回值，重定向到分页查询页面
     */

    @RequestMapping("/edit")
    public String edit(Contract contract){
        //赋值当前企业ID与企业名称
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());
        //判断是否有ID值
        if (StringUtils.isEmpty(contract.getId())){
            //获取当前登录用户
            User user = getLoginUser();
            //设置购销合同创建人的ID和创建部门ID
            contract.setCreateBy(user.getId());
            contract.setCreateDept(user.getDeptId());
            //ID为空执行保存方法
            contractService.save(contract);
        }else {
            //存在ID值  执行修改方法
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }

    /*
        根据ID删除购销合同
     */
    @RequestMapping("/delete")
    public String delete(String id){
        //调用contractService层根据ID删除购销合同
        contractService.delete(id);
        //返回分页查询页面
        return "redirect:/cargo/contract/list.do";
    }

    /*
        修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //调用contractService层根据ID查询购销合同
        Contract contract = contractService.findById(id);
        //将查询到的合同数据保存到request域中
        request.setAttribute("contract",contract);
        //跳转到查询页面
        return "cargo/contract/contract-update";
    }

    /*
        提交合同数据，确认修改数据库状态
        地址：/cargo/contract/submit.do?id=be431e5b-173f-4f59-8dd8-c7d8a3d58fc8
     */
    @RequestMapping("/submit")
    public String submit(String id){
        //根据ID查询数据
        contractService.submit(id);
        return "redirect:/cargo/contract/list.do";
    }

    /*
        取消合同
        地址：/cargo/contract/cancel.do
     */
    @RequestMapping("/cancel")
    public String cancel(String id){
        //根据ID查询合同数据
        contractService.cancel(id);
        return "redirect:/cargo/contract/list.do";
    }
}
